package com.kaibes.web.api;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaibes.core.spring.ApplicationUtils;

public class ApiLinkUtils {
    
    public static boolean isPathValueEmpty(ApiLinkData apiLinkData) {
        return apiLinkData.getPathValues().isEmpty();
    }

    public static String showPathValue(ApiLinkData apiLinkData) {
        StringBuilder sb = new StringBuilder();
        apiLinkData.getPathValues().forEach(value -> sb.append(value.isRequired() ? "*" : "").append("(").append(value.getName())
                .append(")").append(value.getValue()).append("；"));
        return sb.toString();
    }

    public static String showParamValue(ApiLinkData apiLinkData) {
        StringBuilder sb = new StringBuilder();
        apiLinkData.getParamValues().forEach(value -> sb.append(value.isRequired() ? "*" : "").append("(").append(value.getName())
                .append(")").append(value.getValue()).append("；"));
        return sb.toString();
    }
    
    public static String getParamValueString(ApiLinkData apiLinkData) {
        ObjectMapper objectMapper = ApplicationUtils.getInstance().getBean(ObjectMapper.class);
        try {
            String str = objectMapper.writeValueAsString(apiLinkData.getParamValues());
            str = URLEncoder.encode(str, "utf-8");
            return str;
        } catch (JsonProcessingException | UnsupportedEncodingException e) {
            return null;
        }
    }
    public static String getPathValueString(ApiLinkData apiLinkData) {
        ObjectMapper objectMapper = ApplicationUtils.getInstance().getBean(ObjectMapper.class);
        try {
            String str = objectMapper.writeValueAsString(apiLinkData.getPathValues());
            str = URLEncoder.encode(str, "utf-8");
            return str;
        } catch (JsonProcessingException | UnsupportedEncodingException e) {
            return null;
        }
    }
}
